/**
 * 称重仪表读取信息设置
 */

import { FC, useEffect } from "react"
import { Form, Input, message, Modal } from "antd"
import { ISensorType, getGbSystemSetWeightRead } from "../../api"
import { useWindowFontSize } from "../../../../../assets/fun/Antd/configprovider"


const WeightRead: FC<{
    listData?: ISensorType
    handleCallBack: () => void
    addShow:boolean
}> = ({
    listData, handleCallBack,addShow
}) => {
        const [form] = Form.useForm();
        const { validateFields, resetFields, setFieldsValue } = form;
        const handleOk = async () => {
            const values = await validateFields();
            const { code, msg } = (await getGbSystemSetWeightRead({
                ...values,
                id: listData?.id,
            })) as {
                code: number
                msg: string
            }
            if (code === 20000) {
                message.success("设置成功")
                resetFields()
                handleCallBack()
            } else {
                message.error(msg)
            }

        }

        const handleCancel = async () => {
            handleCallBack()
        }

        useEffect(() => {
            if (listData) {
                setFieldsValue(listData)
            }
        }, [listData, setFieldsValue])

        return (
            <>
                <Modal
                    width={useWindowFontSize(520)}
                    title={"设置参数"}
                    open={addShow}
                    onOk={handleOk}
                    onCancel={handleCancel}
                    getContainer={false}
                    >
                    <Form form={form}
                        labelCol={{ span: 8 }}
                        wrapperCol={{ span: 16 }}
                        labelAlign="left"
                    >
                        <Form.Item
                            label="设备序列号"
                            name="deviceNumber"
                            rules={[{ required: true, message: "请填写设备序列号!" }]}
                        >
                            <Input
                                placeholder="请填写设备序列号"
                            />
                        </Form.Item>
                        <Form.Item
                            label="IP地址"
                            name="ipAddress"
                            rules={[{ required: true, message: "请填写IP地址!" }]}
                        >
                            <Input
                                placeholder="请填写IP地址"
                            />
                        </Form.Item>
                        <Form.Item
                            label="SDK端口号"
                            name="sdkPort"
                            rules={[{ required: true, message: "请填写SDK端口号!" },
                                ({ getFieldValue }) => ({
                                    validator(_, value) {
                                        if (value < 0 || value > 65535) {
                                            return Promise.reject("端口号只允许填写0-65535之间的数字!");
                                        }
                                        return Promise.resolve();
                                    },
                                }),
                            ]}
                        >
                            <Input
                                type="number"
                                placeholder="请填写SDK端口号"
                            />
                        </Form.Item>
                        <Form.Item
                            label="用户名"
                            name="userName"
                            rules={[{ required: true, message: "请填写认证用户名!" }]}
                        >
                            <Input 
                                placeholder="请填写认证用户名"
                            />
                        </Form.Item>
                        <Form.Item
                            label="密码"
                            name="password"
                            rules={[{ required: true, message: "请填写密码!" }]}
                        >
                            <Input 
                                type="password"
                                placeholder="请填写认证用户密码"
                            />
                        </Form.Item>
                        <Form.Item
                            label="道闸端口号"
                            name="barrierPort"
                            rules={[{ required: true, message: "请填写道闸端口号!" }]}
                        >
                            <Input 
                                placeholder="请填写道闸端口号"
                            />
                        </Form.Item>
                    </Form>
                </Modal>
            </>
        )
    }

export default WeightRead